define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vote/index' + location.search,
                    add_url: 'vote/add',
                    edit_url: 'vote/edit',
                    del_url: 'vote/del',
                    multi_url: 'vote/multi',
                    table: 'vote',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'user_id', title: __('User_id')},
                        //{field: 'site_id', title: __('Site_id')},
                        //{field: 'user.username', title: __('User.username')},
                        {field: 'site.sitename', title: __('Site.sitename')},
                        {field: 'typedata', title: __('Typedata'), searchList: {"1":__('Typedata 1'),"2":__('Typedata 2'),"3":__('Typedata 3'),"4":__('Typedata 4')}, formatter: Table.api.formatter.normal},
                        {field: 'title', title: __('Title')},
                        {field: 'vote_limit', title: __('Vote_limit')},
                        {field: 'browse', title: __('Browse')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'publishdata', title: __('Publishdata'), searchList: {"0":__('Publishdata 0'),"1":__('Publishdata 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('投票详情'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: function (row) {
                                        if(row.typedata==1){
                                            return '选举详情';
                                        }else if(row.typedata==2){
                                            return '评议详情';
                                        }else if(row.typedata==3){
                                            return '活动详情';
                                        }else if(row.typedata==4){
                                            return '征集详情';
                                        }
                                    },
                                    title: function (row) {
                                        if(row.typedata==1){
                                            return '选举详情';
                                        }else if(row.typedata==2){
                                            return '评议详情';
                                        }else if(row.typedata==3){
                                            return '活动详情';
                                        }else if(row.typedata==4){
                                            return '征集详情';
                                        }
                                    },
                                    extend: 'data-area=["98%","98%"]', //这是控制弹窗的弹出的尺寸
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        if (row.typedata == 1) {
                                            return 'campaign/vote?vote_id='+row.id+'&site_id='+row.site_id;
                                        } else if (row.typedata == 2) {
                                            return 'child_vote/vote?vote_id='+row.id+'&site_id='+row.site_id;
                                        }else if (row.typedata == 3) {
                                            return 'child_vote/vote?vote_id='+row.id+'&site_id='+row.site_id;
                                        }else if (row.typedata == 4) {
                                            return 'child_vote/vote?vote_id='+row.id+'&site_id='+row.site_id;
                                        }
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});